<?php

namespace app\modules\controllers;

use app\modules\models\tag\TagModel;
use Yii;
use app\modules\models\Admin;
use yii\data\Pagination;
use app\modules\models\Rbac;
use yii\db\Query;

class ManageController extends CommonController
{
    protected $mustlogin = ['assign', 'managers', 'reg', 'del', 'changeemail', 'changepass','phone'];

    public function actionManagers()
    {
        $this->layout = "layout1";
        $model = Admin::find();

        $adminuser=Yii::$app->request->get('adminuser');
        $startTime=Yii::$app->request->get('startTime');
        $endTime=Yii::$app->request->get('endTime');
        $where_str='true ';
        if(isset($adminuser)&&!empty($adminuser)){
            $where_str.="and adminuser like '%$adminuser%' ";
        }
        if(isset($startTime)&&!empty($startTime)){
            $startTime=strtotime($startTime);
            $where_str.="and createtime > $startTime ";
        }
        if(isset($endTime)&&!empty($endTime)){
            $endTime=strtotime($endTime);
            $where_str.="and createtime < $endTime ";
        }
        $count = $model->where($where_str)->count();
        $pageSize=$this->pagesize('managesize');
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $pageurl= $this->pageurl('manage/managers');
        $managers = $model->offset($pager->offset)->limit($pager->limit)->where($where_str)->orderBy('adminid DESC')->all();
        return $this->render("managers", ['managers' => $managers, 'pager' => $pager,'pageurl'=>$pageurl,'total'=>$count,'pagesize'=>$pageSize]);
    }

    public function actionReg()
    {
        $this->layout = 'layout1';
        $model = new Admin;
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->reg($post)) {
                Yii::$app->session->setFlash('info', '添加成功');
            } else {
                Yii::$app->session->setFlash('info', '添加失败');
            }
        }
        $model->adminpass = '';
        $model->repass = '';
        return $this->render('reg', ['model' => $model]);
    }

    public function actionDel()
    {
        $adminid = (int)Yii::$app->request->get("adminid");
        if (empty($adminid) || $adminid == 1) {
            $this->redirect(['manage/managers']);
            return false;
        }
        $auth = Yii::$app->authManager;
        $auth->revokeAll($adminid);//移除所有权限
        $model = new Admin;
        if ($model->deleteAll('adminid = :id', [':id' => $adminid])) {
            Yii::$app->session->setFlash('info', '注销成功');
            $this->redirect(['manage/managers']);
        }
    }

    public function actionChangeemail()
    {
        $this->layout = 'layout1';
        $model = Admin::find()->where('adminid = :id', [':id' => Yii::$app->admin->id])->one();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->changeemail($post)) {
                Yii::$app->session->setFlash('info', '修改成功');
            }
        }
        $model->adminpass = "";
        return $this->render('changeemail', ['model' => $model]);
    }

    public function actionChangepass()
    {
        $this->layout = "layout1";
        $model = Admin::find()->where('adminid = :id', [':id' => Yii::$app->admin->id])->one();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->changepass($post)) {
                Yii::$app->session->setFlash('info', '修改成功');
            }
        }
        $model->adminpass = '';
        $model->repass = '';
        return $this->render('changepass', ['model' => $model]);
    }

    public function actionAssign($adminid)
    {
        $adminid = (int)$adminid;
        if (empty($adminid)) {
            throw new \Exception('参数错误');
        }
        $admin = Admin::findOne($adminid);
        if (empty($admin)) {
            throw new \yii\web\NotFoundHttpException('admin not found');
        }

        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            $children = !empty($post['children']) ? $post['children'] : [];
            if (Rbac::grant($adminid, $children)) {
                Yii::$app->session->setFlash('info', '授权成功');
            }
        }

        $auth = Yii::$app->authManager;
        $roles = Rbac::getOptions($auth->getRoles(), null);
        $permissions = Rbac::getOptions($auth->getPermissions(), null);
        $children = Rbac::getChildrenByUser($adminid);

        return $this->render('_assign', ['children' => $children, 'roles' => $roles, 'permissions' => $permissions, 'admin' => $admin->adminuser]);
    }

    //ajax
    public function actionPhone(){
        $post=Yii::$app->request->post();
        $model= new TagModel();
        $data['pageNo']=1;
        $data['pageSize']=1;
        $data['phone']=$post['phone'];
        $res=$model->getUserId($data);
        if($res['http_status_code']==200){
            echo $res['data'][0]['renterUserId'];
            exit;
        }else{
            echo $res['message'];
            die;
        }
    }
}
